package cn.itcast.oa.base;

import java.lang.reflect.ParameterizedType;

import javax.annotation.Resource;

import cn.itcast.oa.domain.User;
import cn.itcast.oa.service.DepartmentService;
import cn.itcast.oa.service.ForumService;
import cn.itcast.oa.service.PrivilegeService;
import cn.itcast.oa.service.ReplyService;
import cn.itcast.oa.service.RoleService;
import cn.itcast.oa.service.TopicService;
import cn.itcast.oa.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{
	
//=================ModelDriven的支持=================
	
	

	private static final long serialVersionUID = 1L;

	protected T model;
	
	protected int pageNum = 1; // 当前页
	protected int pageSize = 10; // 没页显示多少条记录

protected User getCurrentUser() {
	return (User) ActionContext.getContext().getSession().get("user");
}

public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
@SuppressWarnings("unchecked")
public 	BaseAction(){
    //通过反射获取model的真实类型
	try{
	ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
	Class<T> clazz= (Class<T>) pt.getActualTypeArguments()[0];
	//通过反射创建model的实例
	model=clazz.newInstance();
	}catch(Exception e){
		throw new RuntimeException();
	}
}	
	public T getModel() {
		return model;
	}
//==============service实例的声明================
	
	@Resource
	protected RoleService roleService;
	
	@Resource
	protected DepartmentService departmentService;
	
	@Resource
	protected UserService userService;
	
	@Resource
	protected PrivilegeService privilegeService;
	
	@Resource
	protected ForumService forumService;
	
	@Resource
	protected TopicService topicService;
	
	@Resource
	protected ReplyService replyService;
}

